package com.example.demo.service;

import com.example.demo.constants.Constants;
import com.example.demo.Mapper.UserInfoMapper;
import com.example.demo.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    public Boolean checkUserAndPassword(String userName, String password, HttpSession session){

        //账号, 密码为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //在数据库中查询是否存在用户名
        UserInfo userInfo = userInfoMapper.queryUserByName(userName);
        if(userInfo == null){
            return false;
        }

        if( password.equals(userInfo.getPassword())){
            //存储在Session中
            //账号密码正确
            session.setAttribute(Constants.SESSION_USER_KEY,userName);
            return true;
        }
        return false;
    }
}
